//
//  DiscoveryController.m
//  MyCloudMusic
//
//  Created by xiaolong on 2023/12/17.
//

#import "DiscoveryController.h"
#import "BannerData.h"
#import "BanerClickEvent.h"
#import "DiscoveryButtonCell.h"
#import "ButtonData.h"
#import "SheetGroupCell.h"
#import "SongGroupCell.h"
#import "SuperWebController.h"
@interface DiscoveryController ()<SheetGroupDelegate>

@end

@implementation DiscoveryController

- (void)initViews{
    [super initViews];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initTableViewSafeArea];
    
    [self.tableView registerClass:[BannerCell class] forCellReuseIdentifier:BannerCellName];
    
    [self.tableView registerClass:[DiscoveryButtonCell class] forCellReuseIdentifier:DiscoveryBtnCellName];
    
    [self.tableView registerClass:[SheetGroupCell class ] forCellReuseIdentifier:SheetGroupCellName];
    
    [self.tableView registerClass:[SongGroupCell class ] forCellReuseIdentifier:SongGroupCellName];
    
    
}

- (void)initDatum{
    [super initDatum];
    [self loadData];
}

- (void)loadData:(BOOL)isPlaceholder{
    [self.datum removeAllObjects];
    
    [[DefaultRepository shared] bannerAdWithController:self success:^(BaseResponse * _Nonnull baseResponse, Meta * _Nonnull meta, NSArray * _Nonnull data) {
        BannerData *bannerData = [BannerData new];
        bannerData.data = data;
        [self.datum addObject: bannerData];
        
        [self.datum addObject: [ButtonData new]];
        
        [self loadSheetData];
    }];
    
}

-(void)loadSheetData{
    [[DefaultRepository shared] sheets:12 controller:self success:^(BaseResponse * _Nonnull baseResponse, Meta * _Nonnull meta, NSArray * _Nonnull data) {
        //添加歌单数据
        SheetData *sheetData=[SheetData new];
        sheetData.datum = data;
        [self.datum addObject:sheetData];
        [self LoadSongData];
    }];
}



- (void)LoadSongData{
    [[DefaultRepository shared] songsWithController:self success:^(BaseResponse * _Nonnull baseResponse, Meta * _Nonnull meta, NSArray * _Nonnull data) {
        SongData *result = [SongData new];
        result.datum = data;
        [self.datum addObject:result];
        [self.tableView reloadData];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSObject *data = self.datum[indexPath.row];
    ListStyle style = [self typeForItemAtIndexPath:indexPath];
    
    switch (style) {
        case StyleBanner:
        {
            BannerCell *cell = [tableView dequeueReusableCellWithIdentifier:BannerCellName forIndexPath:indexPath];
            [cell bind:data];
            return cell;
        }
            
        case StyleButton:
        {
            DiscoveryButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:DiscoveryBtnCellName forIndexPath:indexPath];
            [cell bind:data];
            return cell;
        }
            
        case StyleSheet:
        {
            SheetGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:SheetGroupCellName forIndexPath:indexPath];
            [cell bind:data];
            cell.delegate = self;
            return cell;
        }
            
        case StyleSong:
        {
            SongGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:SongGroupCellName forIndexPath:indexPath];
            [cell bind:data];
        
            return cell;
        }
        default:
            return nil;
    }
    
   
}

- (void)initListeners{
    [super initListeners];
    @weakify(self);
    [QTSubMain(self, BanerClickEvent) next:^(BanerClickEvent *event){
        @strongify(self);
        [self processAdClick:event.data];
    }];
}

- (void)processAdClick:(Ad *)data{
    NSLog(@"title%@", data.title);
}

- (ListStyle)typeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSObject *data = self.datum[indexPath.row];
    if ([data isKindOfClass:[BannerData class]]) {
        return StyleBanner;
    }
    else if([data isKindOfClass:[ButtonData class]]){
        return StyleButton;
    }
    else if ([data isKindOfClass:[SheetData class]]){
        return StyleSheet;
    }
    else if([data isKindOfClass:[SongData class]]){
        return StyleSong;
    }
    return StyleNone;
}

- (void)sheetClick:(Sheet *)data{
    NSLog(@"xxxxxxx %@", data.title);
    
    [SuperWebController start:self.navigationController
                        title:@"xx" uri:nil];
}

@end
